package main;

import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;

public class Pixel {
	public double x, y, xSpeed, ySpeed;
	Color color;
	public boolean clicked;
	Random random;
	public double weight;

	public Pixel(Color color, int x, int y) {
		this.color = color;
		this.x = x;
		this.y = y;
		clicked = false;
		xSpeed = ySpeed = 0;
		random = new Random();
		weight = 1;
	}
	
	public void drawBorder(Graphics g, Color c){
		g.setColor(c);
		g.drawRoundRect((int)x - 3, (int)y - 3, 7, 7, 2, 2);
	}

	public void draw(Graphics g, int mX, int mY) {
		g.setColor(color);
		g.drawLine((int) x - 1, (int) y - 1, (int) x + 1, (int) y - 1);
		g.drawLine((int) x - 1, (int) y, (int) x + 1, (int) y);
		g.drawLine((int) x - 1, (int) y + 1, (int) x + 1, (int) y + 1);
	}

	public void checkBounds() {
		if (x > 700) {
			x = 700;
			xSpeed = -Math.abs(xSpeed);
		} else if (x < 3) {
			x = 3;
			xSpeed = Math.abs(xSpeed);
		}
		if (y > 498) {
			y = 498;
			ySpeed = -Math.abs(ySpeed) / 3;
			xSpeed = xSpeed * .9;
		}
		if (y < 3) {
			y = 3;
			ySpeed = Math.abs(ySpeed) / 2;
		}
	}
	public void checkSpeed(){
		if (xSpeed > 10) {
			xSpeed = 10;
		}
		if (xSpeed < -10) {
			xSpeed = -10;
		}
		if (ySpeed > 10) {
				ySpeed = 10;
			}
			if (ySpeed < -10) {
				ySpeed = -10;
			}
	}
	public void act(int gravity) {
		
		if (!clicked) {
			ySpeed += (gravity-50)*Math.pow(1.01,gravity-50)/1000.0;
			xSpeed = xSpeed * .992+random.nextDouble()/1000*Math.pow(-1, random.nextInt());
			ySpeed = ySpeed * .995;
		}
		checkSpeed();
		x += xSpeed;
		y += ySpeed;
		checkBounds();
	}

	public void clickAct(int mX, int mY) {
		checkSpeed();
		if (clicked) {
			this.xSpeed = (mX - x);
			this.ySpeed = (mY - y);
		}
		checkSpeed();
		x = mX;
		y = mY;
		checkBounds();
	}

	public void click(Boolean trueFalse) {
		clicked = trueFalse;
	}
}
